/*
 * $Id: DiceRoller.java 11 2009-12-09 04:39:09Z dave.terhune $
 *
 * Copyright (C) 2009 David Terhune
 *
 * This file is part of DiceRoller.
 *
 * DiceRoller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DiceRoller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DiceRoller.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dterhune.gaming.dice;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/** DiceRoller activity. */
public class DiceRoller extends Activity
{
    /** Key to use for setting the most recent view. */
    private static final String VIEW_KEY = "DiceView";

    /** The current view (layout ID). */
    private int currentView;

    /**
     * Called when the activity is first created.
     * @param savedInstanceState previous state information, if any
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null && savedInstanceState.containsKey(VIEW_KEY))
        {
            // Use the previous instance's state
            currentView = savedInstanceState.getInt(VIEW_KEY);
        }
        else
        {
            // Try to get the preferences
            SharedPreferences prefs = getPreferences(MODE_PRIVATE);
            if(prefs != null)
            {
                currentView = prefs.getInt(VIEW_KEY, R.layout.generic);
            }
        }
        setView(currentView);
    }

    /** Called when activity is paused. */
    @Override
    protected void onPause()
    {
        super.onPause();

        // Get the preferences, so we can save them
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();

        // Save the current view to the user preferences
        editor.putInt(VIEW_KEY, currentView);

        // Commit changes
        editor.commit();
    }

    /**
     * Called when state needs to be saved.
     * @param outState bundle in which to save the state information
     */
    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        outState.putInt(VIEW_KEY, currentView);
        super.onSaveInstanceState(outState);
    }

    /**
     * Called when menu button pressed.
     * @param menu menu object to populate
     * @return <code>true</code> if the menu was populated
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    /**
     * Called when menu item selected.
     * @param item menu item that was selected
     * @return <code>true</code> if the menu item was handled
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        boolean handled = false;
        switch(item.getItemId())
        {
            case R.id.menu_burning_wheel:
                currentView = R.layout.burning_wheel;
                handled = true;
                break;

            case R.id.menu_d6:
                currentView = R.layout.generic_d6;
                handled = true;
                break;

            case R.id.menu_fudge:
                currentView = R.layout.fudge;
                handled = true;
                break;

            case R.id.menu_generic:
                currentView = R.layout.generic;
                handled = true;
                break;

            case R.id.menu_hero:
                currentView = R.layout.hero;
                handled = true;
                break;

            case R.id.menu_owod:
                currentView = R.layout.owod;
                handled = true;
                break;

            case R.id.menu_nwod:
                currentView = R.layout.nwod;
                handled = true;
                break;

            default:
                break;
        }
        if(handled)
        {
            // It's being handled.  Set the view to the selected one.
            setView(currentView);
        }
        else
        {
            // Not being handled here.  Pass it up to the base class.
            handled = super.onOptionsItemSelected(item);
        }
        return handled;
    }

    /**
     * Sets the appropriate view for the given ID.
     * @param viewID Layout ID of the view to use
     */
    private void setView(int viewID)
    {
        setContentView(viewID);

        // Get the title and set up the handler for the selected view.
        String layoutName = null;
        switch(viewID)
        {
            case R.layout.burning_wheel:
                new BurningWheelRoller(this);
                layoutName = getString(R.string.bw_name);
                break;
                
            case R.layout.fudge:
                new FudgeRoller(this);
                layoutName = getString(R.string.fudge_name);
                break;

            case R.layout.generic:
                new GenericRoller(this);
                layoutName = getString(R.string.generic_name);
                break;

            case R.layout.generic_d6:
                new D6Roller(this);
                layoutName = getString(R.string.d6_name);
                break;

            case R.layout.hero:
                new HeroRoller(this);
                layoutName = getString(R.string.hero_name);
                break;

            case R.layout.owod:
                new OldWorldOfDarknessRoller(this);
                layoutName = getString(R.string.owod_name);
                break;

            case R.layout.nwod:
                new NewWorldOfDarknessRoller(this);
                layoutName = getString(R.string.nwod_name);
                break;
        }

        // Now set the title to application name + layout name
        setTitle(getString(R.string.app_name) + ": " + layoutName);
    }
}
